<?php
namespace zhtfrontend\controllers;



use common\components\helpers\Fun;
use common\models\datas\CardAttr;
use common\models\datas\CardSend;
use common\models\datas\Customer;
use common\models\datas\IsuserInfo;
use common\models\datas\Student;
use zhtfrontend\components\bases\BaseController;
use zhtfrontend\models\datas\Orders;
use zhtfrontend\models\datas\OrdersItems;

class RenewappointmentController extends BaseController {



    public function actionRenew()
    {
        $ids = Orders::find()->select('id')->where(['order_type'=>'renew','pay_status'=>1])->andWhere(['>','total_amount',0])->column();
        $model = OrdersItems::find()->where(['orders_id'=>$ids,'teach_type'=>1])->all();
        $str = $this->prints($model);
        die($str);
    }

    public function actionAppoint(){
        $rids = Orders::find()->select('id')->where(['order_type'=>'renew','pay_status'=>1])->andWhere(['>','total_amount',0])->column();
        $aids = Orders::find()->select('id')->where(['order_type'=>'appointment','pay_status'=>[1,4]])->andWhere(['>','total_amount',0])->column();
        $model = OrdersItems::find()->where(['orders_id'=>$aids,'teach_type'=>1])->andWhere(['not in','orders_id',$rids])->all();
        $str = $this->prints($model);
        die($str);

    }

    public function prints($model){
        $datas = [];
        foreach ($model as $item){
            $student = Student::findOne($item->student_id);
            $customer = Customer::findOne(isset($student->customer_id)?$student->customer_id:0);
            $card = CardAttr::find()->where(['site_id'=>Fun::getSiteId(),'customer_id'=>isset($customer->id)?$customer->id:0])->one();
            if($card != null){
                $isuser = IsuserInfo::findOne($card->salesman_id);
                $startcardsend = CardSend::find()->select('id')->where(['<=','card_start',$card->card_id])->andWhere(['>','parent_id',0])->andWhere(['is_withdraw'=>0])->column();
                $endcardsend = CardSend::find()->select('id')->where(['>=','card_end',$card->card_id])->andWhere(['>','parent_id',0])->andWhere(['is_withdraw'=>0])->column();
                $intersection = array_intersect($startcardsend,$endcardsend);
                if(!empty($intersection)){
                    foreach ($intersection as $id){
                        $res = CardSend::findOne($id);
                        $twoisuser = IsuserInfo::findOne($res->salesman_id);
                        break;
                    }
                }
            }
            $data['goods_name'] =  $item->name;
            $data['amount'] =  $item->amount;
            $data['customer'] =  isset($customer->name)?$customer->name:'';
            $data['mobile'] =   isset($customer->mobile)?$customer->mobile:'';
            $data['student_name'] =  isset($student->name)?$student->name:'';
            $data['salesman'] = isset($isuser->contact)?$isuser->contact:'';
            $data['twosalesman'] = isset($twoisuser->contact)?$twoisuser->contact:'';
            $data['card_id'] = isset($card->card_id)?$card->card_id:'';
            $datas[] = $data;
        }
        $str = '<table style="border: 1px #e1e1e1 solid; border-collapse: collapse">';
        $str .= '<tr><td align="center">课程</td><td align="center">价格</td><td align="center">会员</td><td align="center">手机号</td><td align="center">学员</td><td align="center">一级发卡人</td><td>二级发卡人</td><td align="center">序号</td></tr>';
        foreach ($datas as $item) {
            $str .= '<tr style="border: 1px #e1e1e1 solid;">';
            $str .= '<td align="center">';
            $str .= $item['goods_name'];
            $str .= '</td>';
            $str .= '<td align="center">';
            $str .= $item['amount'];
            $str .= '</td>';
            $str .= '<td align="center">';
            $str .= $item['customer'];
            $str .= '</td>';
            $str .= '<td align="center">';
            $str .= $item['mobile'];
            $str .= '</td>';
            $str .= '<td align="center">';
            $str .= $item['student_name'];
            $str .= '</td>';
            $str .= '<td align="center">';
            $str .= $item['salesman'];
            $str .= '</td>';
            $str .= '<td align="center">';
            $str .= $item['twosalesman'];
            $str .= '</td>';
            $str .= '<td align="center">';
            $str .= $item['card_id'];
            $str .= '</td>';
            $str .= '</tr>';
        }
        $str .='</table>';
        return $str;
    }



}